/**
 * Created on 20/04/2008
 * 
 */
package com.apbetioli.mapr.place.entity;

import com.apbetioli.mapr.place.schema.CtAddress;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * Persistent entity class for address. 
 *
 * Decided to make this entity embeddable because i don't need to create a 
 * separated primary key. And simplifies deleting, since one line per place 
 * represents all the information. The other purpose is that no address will 
 * be duplicated, neither coordinates, because there won't be 2 markers in 
 * the same place. Simplifying, this is the easyest way to make it run.
 * 
 * The columns are not set with "nullable = false" because this validation 
 * will happen in the xml parsing.
 * 
 * @author Alexandre Parra Betioli
 */
@Embeddable
public class Address extends CtAddress implements Serializable {

    /*---------------------------------------------*/
    // Columns
    /*---------------------------------------------*/
    
    @Column( name="STREET" )
    @Override
    public String getStreet() {
        return super.getStreet(); 
    }
    
    @Column( name="CITY" )
    @Override
    public String getCity() {
        return super.getCity(); 
    }

    @Column( name="UF" )
    @Override
    public String getState() {
        return super.getState();         
    }

    @Column( name="COUNTRY" )
    @Override
    public String getCountry() {
        return super.getCountry();         
    }

    /*---------------------------------------------*/
    // Setters
    /*---------------------------------------------*/
    
    /**
     * Sets the country. If param is null, Brazil is assumed.
     * 
     * @param country The country name.
     */
    @Override
    public void setCountry(String country) {
        if( country == null )
            super.setCountry("Brazil");
        else
            super.setCountry(country);
    }
    
    /*---------------------------------------------*/
    // Methods
    /*---------------------------------------------*/
    
    /**
     * It was necessary because can't cast CtAddress to Address
     */
    public void parse( final CtAddress ctAdress ) {
        if( ctAdress == null )
            throw new IllegalArgumentException("Address is null.");
        
        this.setStreet( ctAdress.getStreet() );
        this.setCity( ctAdress.getCity() );
        this.setState( ctAdress.getState() );
        this.setCountry( ctAdress.getCountry() );
    }
    
    @Override
    public int hashCode() {
        String hash = "" + getStreet() + getCity() + getState() + getCountry();
        return hash.hashCode();
    }

    @Override
    public boolean equals(Object object) {

        if( object == null )
            return false;
        
        if (!(object instanceof Address)) {
            return false;
        }
        
        Address other = (Address) object;
        
        if (((this.getStreet() == null && other.getStreet() != null) || 
             (this.getStreet() != null && !this.getStreet().equals(other.getStreet()))) 
           &&
            ((this.getCity() == null && other.getCity() != null) || 
             (this.getCity() != null && !this.getCity().equals(other.getCity()))) 
           &&
            ((this.getState() == null && other.getState() != null) || 
             (this.getState() != null && !this.getState().equals(other.getState())))              
           &&
            ((this.getCountry() == null && other.getCountry() != null) || 
             (this.getCountry() != null && !this.getCountry().equals(other.getCountry())))                           
           ) {
            return false;
        }
        
        return true;
    }

    /* Used for diplaying the description of the markers in the map */
    @Override
    public String toString() {
        
        StringBuffer res = new StringBuffer();
        
        if( this.getStreet() != null ) {
            res.append( this.getStreet() );
            res.append( "\n" );
        }
        
        res.append( this.getCity() ); 
        res.append( ", " ); 
        res.append( this.getState() );
        res.append( " - " );
        res.append( this.getCountry() );
        res.append( "\n" );
        
        return res.toString();
    }
    
    public String toHtml() {
        return this.toString().replaceAll("\n", "<br>");
    }

    @Override
    public Address clone() {
        Address a = new Address();
        a.setStreet( this.getStreet() );
        a.setCity( this.getCity() );
        a.setState( this.getState() );
        a.setCountry( this.getCountry() );
        return a;
    }

}
